package com.darren.wordcount;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MapperTask implements Runnable {
	private Map<String, Integer> statisticResults = new HashMap<String, Integer>();
	
	private boolean isRunning = true;
	private String fileName;
	
	public MapperTask(String fileName){
		this.fileName = fileName;
	}
	
	
	
	@Override
	public void run() {
System.out.println(Thread.currentThread()+",start......,and fileName:"+fileName);		
		while(isRunning){
			BufferedReader br = null;
			
			try {
				br = new BufferedReader(new FileReader(fileName));
				String line = null;
				
				while((line = br.readLine()) !=	null){
					String[] words = line.split(" ");
					
					for(String word : words){
						if(!statisticResults.containsKey(word))
							statisticResults.put(word, 1);
						else
							statisticResults.put(word, statisticResults.get(word)+1);
					}
				}
				isRunning = false;
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			
		}
	}
	
	
	public boolean isRunning(){
		return this.isRunning;
	}
	
	public Map<String, Integer> getResult(){
		return statisticResults;
	}
	

}
